package com.faf.ui;

import java.io.IOException;
import java.sql.SQLException;
import java.util.concurrent.TimeUnit;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.faf.dao.impl.UserDaoImpl;
import com.faf.tools.RSAUtils;

/**
 * Servlet implementation class UserManagerment
 */
@WebServlet("/UserManagement")
public class UserManagement extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserManagement() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**+
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//设置session
		   HttpSession session = request.getSession();
		//设置中文格式
		response.setContentType("text/html;charset=utf-8"); 
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		//获取user_center_update.jsp
		String realName = request.getParameter("realName");
		String account = request.getParameter("account");
		String password = request.getParameter("password");
		password = RSAUtils.publicEncrytype(password);
		System.out.println("密码"+password);
		String pwd_rsa = RSAUtils.privateDecrypt(password);
		String sex = request.getParameter("sex");
		System.out.println("存储性别为"+sex);
		String age = request.getParameter("age");
		String homeTown = request.getParameter("homeTown");
		String address = request.getParameter("address");
		String phone = request.getParameter("phone");
		//定义sql语句
		String sql_ones = "update user set userPassword=? , userName=? , userSex=? , userAge=? , userhometown=? , userAddress=? , userPhone=? where userNumber=? ";
		//查询
		UserDaoImpl user = new UserDaoImpl();
		try {
			user.executeUpdate(sql_ones, password,realName,sex,age,homeTown,address,phone,account);//更新用户信息的数据库
			session.setAttribute("name",realName);
			session.setAttribute("phone",phone);
			session.setAttribute("password",pwd_rsa);
			session.setAttribute("account",account);
			session.setAttribute("sex",sex);
			session.setAttribute("hometown",homeTown);
			session.setAttribute("address",address);
			session.setAttribute("age",age);
			System.out.println("用户信息更新成功");
			
			TimeUnit.MILLISECONDS.sleep(1000);//MILLISECONDS表示以毫秒为单位延时
			//更新之后保留页面内容
			String url="/FAFCode/UserManagement/user_center_update.jsp";
			response.sendRedirect(url);
			
				//更新之后保留页面内容
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("更新失败");
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
	}
}
